Intro
- How do you usually share data analyses with your collaborators? Many people share them through Word document, spreadsheets, attachments, etc.
- In R Markdown, you can incorporate ordinary text (ex. experimental methods, analysis and discussion of results) alongside code and figures! (Some people write entire manuscripts in R Markdown.)
- This is useful for writing reproducible analysis reports, publication, sharing work with collaborators, writing up homework, and keeping a bioinformatics notebook.
- Because the code is emedded in the document, the figures are reproducible.
- If you find an error or want to add more to the report, you can just re-run the document and you’ll have updated figures!
- This concept of combining text and code is called “literate programming”.
- To do this we use R Markdown - combines Markdown (renders plain text) with R.
Creating an R Markdown file
- Open RStudio
- File → New File → R Markdown
- Give your document a title
- Keep default output format as HTML. You need TeX installed if you want to generate PDF documents.
- R Markdown files always end in
.Rmd
Basic components of R Markdown
- Header: instructions for R to specify type of document to be created and options to choose (ex. title, author, date)
- Key-value pairs
- At start of file
- Between lines of
---
---
title: 'Literate Programming: Creating reproducible reports using R Markdown'
author: "Zena Lapp"
date: "4/20/2019"
output: html_document
---
- Setup (technically R code): sets up options for all code chunks (ex. show code in output file)
- Text: narration formatted with markdown
- Code chunks: embedded code
```{r}
# Your code here
```
- R Markdown uses the location of the
.Rmd file as the working directory (where you are on your computer)
Markdown
Text formatting and bullet points
- Bold text with double asterisks like this
- Italicize text with underscores like this
- Add code-type font with backticks
like this
- Make a list using dashes (
-) or asterisks (*)
- You can make a nubmered list using
1. for each thing in your list (like the example below)
Lists
Steps to generate an R Markdown file:
- Open a .Rmd file in RStudio
- Write text and code
- Compile using Knit!
Including images
- You can include an image file like this:
